package com.tpp.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tpp.comm.pojo.TpLinkUser;
import com.tpp.comm.util.JwtUtil;
import com.tpp.user.mapper.LinkUserMapper;
import com.tpp.user.service.LinkUserService;
import com.tpp.user.vo.LinkUserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class LinkUserServiceImpl implements LinkUserService {

    @Resource
    private LinkUserMapper linkUserMapper;

    @Resource
    private JwtUtil jwtUtil;

    public List<LinkUserVo> findByToken(String token){
        Integer id = Math.abs(jwtUtil.parseJwt(token));
        LambdaQueryWrapper<TpLinkUser> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(TpLinkUser::getUserId,id);
        List<TpLinkUser> tpLinkUsers = linkUserMapper.selectList(wrapper);
        return tpLinkUsers.stream().map(linkUser->{
            LinkUserVo linkUserVo1 = new LinkUserVo();
            BeanUtils.copyProperties(linkUser,linkUserVo1);
            return linkUserVo1;
        }).collect(Collectors.toList());
    }

}
